{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 期中测验（Numpy, Pandas）\n",
    "姓名：    学号：   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. 编写程序将“部分公司的部分财务数据.csv”读入到Pandas的DataFrame里"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'encoding': 'GB2312', 'confidence': 0.99, 'language': 'Chinese'}"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import chardet\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "f = open(\"部分公司的部分财务数据.csv\", \"rb\")\n",
    "chardet.detect(f.read())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. 编写程序对公司代码、交易日期建立二级索引，同时对公司代码、交易日期从小到大排序。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>现金流价格比率</th>\n",
       "      <th>公司账面市值比</th>\n",
       "      <th>收益价格比</th>\n",
       "      <th>研发成本收入比</th>\n",
       "      <th>净资产收益率</th>\n",
       "      <th>净资产增长率</th>\n",
       "      <th>营收增长率</th>\n",
       "      <th>营业利润增长率</th>\n",
       "      <th>净利润增长率</th>\n",
       "      <th>现金资产比</th>\n",
       "      <th>负债增长率</th>\n",
       "      <th>year</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>公司代码</th>\n",
       "      <th>财报日期</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">000002.SZ</th>\n",
       "      <th>20080131</th>\n",
       "      <td>0.065044</td>\n",
       "      <td>0.145823</td>\n",
       "      <td>0.011132</td>\n",
       "      <td>0.069587</td>\n",
       "      <td>9.3431</td>\n",
       "      <td>162.4120</td>\n",
       "      <td>67.3176</td>\n",
       "      <td>92.1256</td>\n",
       "      <td>61.1962</td>\n",
       "      <td>0.326183</td>\n",
       "      <td>1.152008</td>\n",
       "      <td>2008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20080229</th>\n",
       "      <td>0.072877</td>\n",
       "      <td>0.163385</td>\n",
       "      <td>0.012473</td>\n",
       "      <td>0.069587</td>\n",
       "      <td>9.3431</td>\n",
       "      <td>162.4120</td>\n",
       "      <td>67.3176</td>\n",
       "      <td>92.1256</td>\n",
       "      <td>61.1962</td>\n",
       "      <td>0.326183</td>\n",
       "      <td>1.152008</td>\n",
       "      <td>2008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20080331</th>\n",
       "      <td>0.066187</td>\n",
       "      <td>0.148387</td>\n",
       "      <td>0.011328</td>\n",
       "      <td>0.069587</td>\n",
       "      <td>9.3431</td>\n",
       "      <td>162.4120</td>\n",
       "      <td>67.3176</td>\n",
       "      <td>92.1256</td>\n",
       "      <td>61.1962</td>\n",
       "      <td>0.326183</td>\n",
       "      <td>1.152008</td>\n",
       "      <td>2008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20080430</th>\n",
       "      <td>0.037636</td>\n",
       "      <td>0.174830</td>\n",
       "      <td>0.029955</td>\n",
       "      <td>0.049646</td>\n",
       "      <td>21.9160</td>\n",
       "      <td>96.1252</td>\n",
       "      <td>126.9287</td>\n",
       "      <td>238.2730</td>\n",
       "      <td>233.8710</td>\n",
       "      <td>0.227265</td>\n",
       "      <td>1.038263</td>\n",
       "      <td>2008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20080530</th>\n",
       "      <td>0.046441</td>\n",
       "      <td>0.215727</td>\n",
       "      <td>0.036962</td>\n",
       "      <td>0.049646</td>\n",
       "      <td>21.9160</td>\n",
       "      <td>96.1252</td>\n",
       "      <td>126.9287</td>\n",
       "      <td>238.2730</td>\n",
       "      <td>233.8710</td>\n",
       "      <td>0.227265</td>\n",
       "      <td>1.038263</td>\n",
       "      <td>2008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">000021.SZ</th>\n",
       "      <th>20120831</th>\n",
       "      <td>-0.001807</td>\n",
       "      <td>0.615462</td>\n",
       "      <td>0.010287</td>\n",
       "      <td>0.016030</td>\n",
       "      <td>1.6515</td>\n",
       "      <td>-1.0369</td>\n",
       "      <td>-2.0982</td>\n",
       "      <td>-72.0864</td>\n",
       "      <td>-84.2320</td>\n",
       "      <td>0.525017</td>\n",
       "      <td>0.829568</td>\n",
       "      <td>2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20120928</th>\n",
       "      <td>-0.001781</td>\n",
       "      <td>0.606741</td>\n",
       "      <td>0.010142</td>\n",
       "      <td>0.016030</td>\n",
       "      <td>1.6515</td>\n",
       "      <td>-1.0369</td>\n",
       "      <td>-2.0982</td>\n",
       "      <td>-72.0864</td>\n",
       "      <td>-84.2320</td>\n",
       "      <td>0.525017</td>\n",
       "      <td>0.829568</td>\n",
       "      <td>2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20121031</th>\n",
       "      <td>-0.088502</td>\n",
       "      <td>0.632025</td>\n",
       "      <td>0.011371</td>\n",
       "      <td>0.018754</td>\n",
       "      <td>1.7769</td>\n",
       "      <td>-1.4100</td>\n",
       "      <td>-17.4595</td>\n",
       "      <td>-130.9577</td>\n",
       "      <td>-121.6043</td>\n",
       "      <td>0.429067</td>\n",
       "      <td>0.136592</td>\n",
       "      <td>2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20121130</th>\n",
       "      <td>-0.101329</td>\n",
       "      <td>0.723623</td>\n",
       "      <td>0.013019</td>\n",
       "      <td>0.018754</td>\n",
       "      <td>1.7769</td>\n",
       "      <td>-1.4100</td>\n",
       "      <td>-17.4595</td>\n",
       "      <td>-130.9577</td>\n",
       "      <td>-121.6043</td>\n",
       "      <td>0.429067</td>\n",
       "      <td>0.136592</td>\n",
       "      <td>2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20121231</th>\n",
       "      <td>-0.089066</td>\n",
       "      <td>0.636051</td>\n",
       "      <td>0.011444</td>\n",
       "      <td>0.018754</td>\n",
       "      <td>1.7769</td>\n",
       "      <td>-1.4100</td>\n",
       "      <td>-17.4595</td>\n",
       "      <td>-130.9577</td>\n",
       "      <td>-121.6043</td>\n",
       "      <td>0.429067</td>\n",
       "      <td>0.136592</td>\n",
       "      <td>2012</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>371 rows × 12 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                     现金流价格比率   公司账面市值比     收益价格比   研发成本收入比   净资产收益率    净资产增长率  \\\n",
       "公司代码      财报日期                                                                  \n",
       "000002.SZ 20080131  0.065044  0.145823  0.011132  0.069587   9.3431  162.4120   \n",
       "          20080229  0.072877  0.163385  0.012473  0.069587   9.3431  162.4120   \n",
       "          20080331  0.066187  0.148387  0.011328  0.069587   9.3431  162.4120   \n",
       "          20080430  0.037636  0.174830  0.029955  0.049646  21.9160   96.1252   \n",
       "          20080530  0.046441  0.215727  0.036962  0.049646  21.9160   96.1252   \n",
       "...                      ...       ...       ...       ...      ...       ...   \n",
       "000021.SZ 20120831 -0.001807  0.615462  0.010287  0.016030   1.6515   -1.0369   \n",
       "          20120928 -0.001781  0.606741  0.010142  0.016030   1.6515   -1.0369   \n",
       "          20121031 -0.088502  0.632025  0.011371  0.018754   1.7769   -1.4100   \n",
       "          20121130 -0.101329  0.723623  0.013019  0.018754   1.7769   -1.4100   \n",
       "          20121231 -0.089066  0.636051  0.011444  0.018754   1.7769   -1.4100   \n",
       "\n",
       "                       营收增长率   营业利润增长率    净利润增长率     现金资产比     负债增长率  year  \n",
       "公司代码      财报日期                                                              \n",
       "000002.SZ 20080131   67.3176   92.1256   61.1962  0.326183  1.152008  2008  \n",
       "          20080229   67.3176   92.1256   61.1962  0.326183  1.152008  2008  \n",
       "          20080331   67.3176   92.1256   61.1962  0.326183  1.152008  2008  \n",
       "          20080430  126.9287  238.2730  233.8710  0.227265  1.038263  2008  \n",
       "          20080530  126.9287  238.2730  233.8710  0.227265  1.038263  2008  \n",
       "...                      ...       ...       ...       ...       ...   ...  \n",
       "000021.SZ 20120831   -2.0982  -72.0864  -84.2320  0.525017  0.829568  2012  \n",
       "          20120928   -2.0982  -72.0864  -84.2320  0.525017  0.829568  2012  \n",
       "          20121031  -17.4595 -130.9577 -121.6043  0.429067  0.136592  2012  \n",
       "          20121130  -17.4595 -130.9577 -121.6043  0.429067  0.136592  2012  \n",
       "          20121231  -17.4595 -130.9577 -121.6043  0.429067  0.136592  2012  \n",
       "\n",
       "[371 rows x 12 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv(\"部分公司的部分财务数据.csv\", encoding = \"GB2312\")\n",
    "df[\"year\"] = df[\"财报日期\"].apply(lambda x: int(x / 10000))\n",
    "df = df.set_index([\"公司代码\",\"财报日期\"])\n",
    "df = df.sort_index(ascending = True, level = [0,1])\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. 编写程序取出股票代码“000009.SZ”在2012年发布的所有财报数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>公司代码</th>\n",
       "      <th>财报日期</th>\n",
       "      <th>现金流价格比率</th>\n",
       "      <th>公司账面市值比</th>\n",
       "      <th>收益价格比</th>\n",
       "      <th>研发成本收入比</th>\n",
       "      <th>净资产收益率</th>\n",
       "      <th>净资产增长率</th>\n",
       "      <th>营收增长率</th>\n",
       "      <th>营业利润增长率</th>\n",
       "      <th>净利润增长率</th>\n",
       "      <th>现金资产比</th>\n",
       "      <th>负债增长率</th>\n",
       "      <th>year</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>180</th>\n",
       "      <td>000009.SZ</td>\n",
       "      <td>20120131</td>\n",
       "      <td>0.013876</td>\n",
       "      <td>0.186674</td>\n",
       "      <td>0.016279</td>\n",
       "      <td>0.107325</td>\n",
       "      <td>9.0020</td>\n",
       "      <td>8.5343</td>\n",
       "      <td>15.8421</td>\n",
       "      <td>-44.3636</td>\n",
       "      <td>-37.6398</td>\n",
       "      <td>0.172990</td>\n",
       "      <td>0.205289</td>\n",
       "      <td>2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>181</th>\n",
       "      <td>000009.SZ</td>\n",
       "      <td>20120229</td>\n",
       "      <td>0.014541</td>\n",
       "      <td>0.195621</td>\n",
       "      <td>0.017059</td>\n",
       "      <td>0.107325</td>\n",
       "      <td>9.0020</td>\n",
       "      <td>8.5343</td>\n",
       "      <td>15.8421</td>\n",
       "      <td>-44.3636</td>\n",
       "      <td>-37.6398</td>\n",
       "      <td>0.172990</td>\n",
       "      <td>0.205289</td>\n",
       "      <td>2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>182</th>\n",
       "      <td>000009.SZ</td>\n",
       "      <td>20120330</td>\n",
       "      <td>0.017653</td>\n",
       "      <td>0.237485</td>\n",
       "      <td>0.020710</td>\n",
       "      <td>0.107325</td>\n",
       "      <td>9.0020</td>\n",
       "      <td>8.5343</td>\n",
       "      <td>15.8421</td>\n",
       "      <td>-44.3636</td>\n",
       "      <td>-37.6398</td>\n",
       "      <td>0.172990</td>\n",
       "      <td>0.205289</td>\n",
       "      <td>2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>183</th>\n",
       "      <td>000009.SZ</td>\n",
       "      <td>20120427</td>\n",
       "      <td>-0.037266</td>\n",
       "      <td>0.218417</td>\n",
       "      <td>0.022482</td>\n",
       "      <td>0.103504</td>\n",
       "      <td>10.4952</td>\n",
       "      <td>6.2010</td>\n",
       "      <td>54.7177</td>\n",
       "      <td>-20.7534</td>\n",
       "      <td>-62.8931</td>\n",
       "      <td>0.109547</td>\n",
       "      <td>0.214644</td>\n",
       "      <td>2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>184</th>\n",
       "      <td>000009.SZ</td>\n",
       "      <td>20120531</td>\n",
       "      <td>-0.036224</td>\n",
       "      <td>0.212308</td>\n",
       "      <td>0.021853</td>\n",
       "      <td>0.103504</td>\n",
       "      <td>10.4952</td>\n",
       "      <td>6.2010</td>\n",
       "      <td>54.7177</td>\n",
       "      <td>-20.7534</td>\n",
       "      <td>-62.8931</td>\n",
       "      <td>0.109547</td>\n",
       "      <td>0.214644</td>\n",
       "      <td>2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>185</th>\n",
       "      <td>000009.SZ</td>\n",
       "      <td>20120629</td>\n",
       "      <td>-0.041440</td>\n",
       "      <td>0.242880</td>\n",
       "      <td>0.025000</td>\n",
       "      <td>0.103504</td>\n",
       "      <td>10.4952</td>\n",
       "      <td>6.2010</td>\n",
       "      <td>54.7177</td>\n",
       "      <td>-20.7534</td>\n",
       "      <td>-62.8931</td>\n",
       "      <td>0.109547</td>\n",
       "      <td>0.214644</td>\n",
       "      <td>2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>186</th>\n",
       "      <td>000009.SZ</td>\n",
       "      <td>20120731</td>\n",
       "      <td>0.050707</td>\n",
       "      <td>0.313878</td>\n",
       "      <td>0.006098</td>\n",
       "      <td>0.109074</td>\n",
       "      <td>1.9145</td>\n",
       "      <td>6.5500</td>\n",
       "      <td>17.1946</td>\n",
       "      <td>-44.1871</td>\n",
       "      <td>-49.3985</td>\n",
       "      <td>0.160741</td>\n",
       "      <td>0.269783</td>\n",
       "      <td>2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>187</th>\n",
       "      <td>000009.SZ</td>\n",
       "      <td>20120831</td>\n",
       "      <td>0.047250</td>\n",
       "      <td>0.292477</td>\n",
       "      <td>0.005682</td>\n",
       "      <td>0.109074</td>\n",
       "      <td>1.9145</td>\n",
       "      <td>6.5500</td>\n",
       "      <td>17.1946</td>\n",
       "      <td>-44.1871</td>\n",
       "      <td>-49.3985</td>\n",
       "      <td>0.160741</td>\n",
       "      <td>0.269783</td>\n",
       "      <td>2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>188</th>\n",
       "      <td>000009.SZ</td>\n",
       "      <td>20120928</td>\n",
       "      <td>0.046667</td>\n",
       "      <td>0.288866</td>\n",
       "      <td>0.005612</td>\n",
       "      <td>0.109074</td>\n",
       "      <td>1.9145</td>\n",
       "      <td>6.5500</td>\n",
       "      <td>17.1946</td>\n",
       "      <td>-44.1871</td>\n",
       "      <td>-49.3985</td>\n",
       "      <td>0.160741</td>\n",
       "      <td>0.269783</td>\n",
       "      <td>2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>189</th>\n",
       "      <td>000009.SZ</td>\n",
       "      <td>20121031</td>\n",
       "      <td>0.072740</td>\n",
       "      <td>0.297717</td>\n",
       "      <td>0.006107</td>\n",
       "      <td>0.112089</td>\n",
       "      <td>2.1248</td>\n",
       "      <td>8.2972</td>\n",
       "      <td>29.9011</td>\n",
       "      <td>-43.6262</td>\n",
       "      <td>-37.2151</td>\n",
       "      <td>0.172482</td>\n",
       "      <td>0.161037</td>\n",
       "      <td>2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>190</th>\n",
       "      <td>000009.SZ</td>\n",
       "      <td>20121130</td>\n",
       "      <td>0.082753</td>\n",
       "      <td>0.338701</td>\n",
       "      <td>0.006948</td>\n",
       "      <td>0.112089</td>\n",
       "      <td>2.1248</td>\n",
       "      <td>8.2972</td>\n",
       "      <td>29.9011</td>\n",
       "      <td>-43.6262</td>\n",
       "      <td>-37.2151</td>\n",
       "      <td>0.172482</td>\n",
       "      <td>0.161037</td>\n",
       "      <td>2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>191</th>\n",
       "      <td>000009.SZ</td>\n",
       "      <td>20121231</td>\n",
       "      <td>0.072409</td>\n",
       "      <td>0.296364</td>\n",
       "      <td>0.006080</td>\n",
       "      <td>0.112089</td>\n",
       "      <td>2.1248</td>\n",
       "      <td>8.2972</td>\n",
       "      <td>29.9011</td>\n",
       "      <td>-43.6262</td>\n",
       "      <td>-37.2151</td>\n",
       "      <td>0.172482</td>\n",
       "      <td>0.161037</td>\n",
       "      <td>2012</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          公司代码      财报日期   现金流价格比率   公司账面市值比     收益价格比   研发成本收入比   净资产收益率  \\\n",
       "180  000009.SZ  20120131  0.013876  0.186674  0.016279  0.107325   9.0020   \n",
       "181  000009.SZ  20120229  0.014541  0.195621  0.017059  0.107325   9.0020   \n",
       "182  000009.SZ  20120330  0.017653  0.237485  0.020710  0.107325   9.0020   \n",
       "183  000009.SZ  20120427 -0.037266  0.218417  0.022482  0.103504  10.4952   \n",
       "184  000009.SZ  20120531 -0.036224  0.212308  0.021853  0.103504  10.4952   \n",
       "185  000009.SZ  20120629 -0.041440  0.242880  0.025000  0.103504  10.4952   \n",
       "186  000009.SZ  20120731  0.050707  0.313878  0.006098  0.109074   1.9145   \n",
       "187  000009.SZ  20120831  0.047250  0.292477  0.005682  0.109074   1.9145   \n",
       "188  000009.SZ  20120928  0.046667  0.288866  0.005612  0.109074   1.9145   \n",
       "189  000009.SZ  20121031  0.072740  0.297717  0.006107  0.112089   2.1248   \n",
       "190  000009.SZ  20121130  0.082753  0.338701  0.006948  0.112089   2.1248   \n",
       "191  000009.SZ  20121231  0.072409  0.296364  0.006080  0.112089   2.1248   \n",
       "\n",
       "     净资产增长率    营收增长率  营业利润增长率   净利润增长率     现金资产比     负债增长率  year  \n",
       "180  8.5343  15.8421 -44.3636 -37.6398  0.172990  0.205289  2012  \n",
       "181  8.5343  15.8421 -44.3636 -37.6398  0.172990  0.205289  2012  \n",
       "182  8.5343  15.8421 -44.3636 -37.6398  0.172990  0.205289  2012  \n",
       "183  6.2010  54.7177 -20.7534 -62.8931  0.109547  0.214644  2012  \n",
       "184  6.2010  54.7177 -20.7534 -62.8931  0.109547  0.214644  2012  \n",
       "185  6.2010  54.7177 -20.7534 -62.8931  0.109547  0.214644  2012  \n",
       "186  6.5500  17.1946 -44.1871 -49.3985  0.160741  0.269783  2012  \n",
       "187  6.5500  17.1946 -44.1871 -49.3985  0.160741  0.269783  2012  \n",
       "188  6.5500  17.1946 -44.1871 -49.3985  0.160741  0.269783  2012  \n",
       "189  8.2972  29.9011 -43.6262 -37.2151  0.172482  0.161037  2012  \n",
       "190  8.2972  29.9011 -43.6262 -37.2151  0.172482  0.161037  2012  \n",
       "191  8.2972  29.9011 -43.6262 -37.2151  0.172482  0.161037  2012  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = df.reset_index()\n",
    "ans_df = df[(df[\"year\"] == 2012 )& (df[\"公司代码\"] == \"000009.SZ\")]\n",
    "ans_df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4. 编写程序打印出数据中的缺失值的位置；然后删除有缺失值的所在行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>公司代码</th>\n",
       "      <th>财报日期</th>\n",
       "      <th>现金流价格比率</th>\n",
       "      <th>公司账面市值比</th>\n",
       "      <th>收益价格比</th>\n",
       "      <th>研发成本收入比</th>\n",
       "      <th>净资产收益率</th>\n",
       "      <th>净资产增长率</th>\n",
       "      <th>营收增长率</th>\n",
       "      <th>营业利润增长率</th>\n",
       "      <th>净利润增长率</th>\n",
       "      <th>现金资产比</th>\n",
       "      <th>负债增长率</th>\n",
       "      <th>year</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>000002.SZ</td>\n",
       "      <td>20080131</td>\n",
       "      <td>0.065044</td>\n",
       "      <td>0.145823</td>\n",
       "      <td>0.011132</td>\n",
       "      <td>0.069587</td>\n",
       "      <td>9.3431</td>\n",
       "      <td>162.4120</td>\n",
       "      <td>67.3176</td>\n",
       "      <td>92.1256</td>\n",
       "      <td>61.1962</td>\n",
       "      <td>0.326183</td>\n",
       "      <td>1.152008</td>\n",
       "      <td>2008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>000002.SZ</td>\n",
       "      <td>20080229</td>\n",
       "      <td>0.072877</td>\n",
       "      <td>0.163385</td>\n",
       "      <td>0.012473</td>\n",
       "      <td>0.069587</td>\n",
       "      <td>9.3431</td>\n",
       "      <td>162.4120</td>\n",
       "      <td>67.3176</td>\n",
       "      <td>92.1256</td>\n",
       "      <td>61.1962</td>\n",
       "      <td>0.326183</td>\n",
       "      <td>1.152008</td>\n",
       "      <td>2008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>000002.SZ</td>\n",
       "      <td>20080331</td>\n",
       "      <td>0.066187</td>\n",
       "      <td>0.148387</td>\n",
       "      <td>0.011328</td>\n",
       "      <td>0.069587</td>\n",
       "      <td>9.3431</td>\n",
       "      <td>162.4120</td>\n",
       "      <td>67.3176</td>\n",
       "      <td>92.1256</td>\n",
       "      <td>61.1962</td>\n",
       "      <td>0.326183</td>\n",
       "      <td>1.152008</td>\n",
       "      <td>2008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>000002.SZ</td>\n",
       "      <td>20080430</td>\n",
       "      <td>0.037636</td>\n",
       "      <td>0.174830</td>\n",
       "      <td>0.029955</td>\n",
       "      <td>0.049646</td>\n",
       "      <td>21.9160</td>\n",
       "      <td>96.1252</td>\n",
       "      <td>126.9287</td>\n",
       "      <td>238.2730</td>\n",
       "      <td>233.8710</td>\n",
       "      <td>0.227265</td>\n",
       "      <td>1.038263</td>\n",
       "      <td>2008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>000002.SZ</td>\n",
       "      <td>20080530</td>\n",
       "      <td>0.046441</td>\n",
       "      <td>0.215727</td>\n",
       "      <td>0.036962</td>\n",
       "      <td>0.049646</td>\n",
       "      <td>21.9160</td>\n",
       "      <td>96.1252</td>\n",
       "      <td>126.9287</td>\n",
       "      <td>238.2730</td>\n",
       "      <td>233.8710</td>\n",
       "      <td>0.227265</td>\n",
       "      <td>1.038263</td>\n",
       "      <td>2008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>366</th>\n",
       "      <td>000021.SZ</td>\n",
       "      <td>20120831</td>\n",
       "      <td>-0.001807</td>\n",
       "      <td>0.615462</td>\n",
       "      <td>0.010287</td>\n",
       "      <td>0.016030</td>\n",
       "      <td>1.6515</td>\n",
       "      <td>-1.0369</td>\n",
       "      <td>-2.0982</td>\n",
       "      <td>-72.0864</td>\n",
       "      <td>-84.2320</td>\n",
       "      <td>0.525017</td>\n",
       "      <td>0.829568</td>\n",
       "      <td>2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>367</th>\n",
       "      <td>000021.SZ</td>\n",
       "      <td>20120928</td>\n",
       "      <td>-0.001781</td>\n",
       "      <td>0.606741</td>\n",
       "      <td>0.010142</td>\n",
       "      <td>0.016030</td>\n",
       "      <td>1.6515</td>\n",
       "      <td>-1.0369</td>\n",
       "      <td>-2.0982</td>\n",
       "      <td>-72.0864</td>\n",
       "      <td>-84.2320</td>\n",
       "      <td>0.525017</td>\n",
       "      <td>0.829568</td>\n",
       "      <td>2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>368</th>\n",
       "      <td>000021.SZ</td>\n",
       "      <td>20121031</td>\n",
       "      <td>-0.088502</td>\n",
       "      <td>0.632025</td>\n",
       "      <td>0.011371</td>\n",
       "      <td>0.018754</td>\n",
       "      <td>1.7769</td>\n",
       "      <td>-1.4100</td>\n",
       "      <td>-17.4595</td>\n",
       "      <td>-130.9577</td>\n",
       "      <td>-121.6043</td>\n",
       "      <td>0.429067</td>\n",
       "      <td>0.136592</td>\n",
       "      <td>2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>369</th>\n",
       "      <td>000021.SZ</td>\n",
       "      <td>20121130</td>\n",
       "      <td>-0.101329</td>\n",
       "      <td>0.723623</td>\n",
       "      <td>0.013019</td>\n",
       "      <td>0.018754</td>\n",
       "      <td>1.7769</td>\n",
       "      <td>-1.4100</td>\n",
       "      <td>-17.4595</td>\n",
       "      <td>-130.9577</td>\n",
       "      <td>-121.6043</td>\n",
       "      <td>0.429067</td>\n",
       "      <td>0.136592</td>\n",
       "      <td>2012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>370</th>\n",
       "      <td>000021.SZ</td>\n",
       "      <td>20121231</td>\n",
       "      <td>-0.089066</td>\n",
       "      <td>0.636051</td>\n",
       "      <td>0.011444</td>\n",
       "      <td>0.018754</td>\n",
       "      <td>1.7769</td>\n",
       "      <td>-1.4100</td>\n",
       "      <td>-17.4595</td>\n",
       "      <td>-130.9577</td>\n",
       "      <td>-121.6043</td>\n",
       "      <td>0.429067</td>\n",
       "      <td>0.136592</td>\n",
       "      <td>2012</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>369 rows × 14 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "          公司代码      财报日期   现金流价格比率   公司账面市值比     收益价格比   研发成本收入比   净资产收益率  \\\n",
       "0    000002.SZ  20080131  0.065044  0.145823  0.011132  0.069587   9.3431   \n",
       "1    000002.SZ  20080229  0.072877  0.163385  0.012473  0.069587   9.3431   \n",
       "2    000002.SZ  20080331  0.066187  0.148387  0.011328  0.069587   9.3431   \n",
       "3    000002.SZ  20080430  0.037636  0.174830  0.029955  0.049646  21.9160   \n",
       "4    000002.SZ  20080530  0.046441  0.215727  0.036962  0.049646  21.9160   \n",
       "..         ...       ...       ...       ...       ...       ...      ...   \n",
       "366  000021.SZ  20120831 -0.001807  0.615462  0.010287  0.016030   1.6515   \n",
       "367  000021.SZ  20120928 -0.001781  0.606741  0.010142  0.016030   1.6515   \n",
       "368  000021.SZ  20121031 -0.088502  0.632025  0.011371  0.018754   1.7769   \n",
       "369  000021.SZ  20121130 -0.101329  0.723623  0.013019  0.018754   1.7769   \n",
       "370  000021.SZ  20121231 -0.089066  0.636051  0.011444  0.018754   1.7769   \n",
       "\n",
       "       净资产增长率     营收增长率   营业利润增长率    净利润增长率     现金资产比     负债增长率  year  \n",
       "0    162.4120   67.3176   92.1256   61.1962  0.326183  1.152008  2008  \n",
       "1    162.4120   67.3176   92.1256   61.1962  0.326183  1.152008  2008  \n",
       "2    162.4120   67.3176   92.1256   61.1962  0.326183  1.152008  2008  \n",
       "3     96.1252  126.9287  238.2730  233.8710  0.227265  1.038263  2008  \n",
       "4     96.1252  126.9287  238.2730  233.8710  0.227265  1.038263  2008  \n",
       "..        ...       ...       ...       ...       ...       ...   ...  \n",
       "366   -1.0369   -2.0982  -72.0864  -84.2320  0.525017  0.829568  2012  \n",
       "367   -1.0369   -2.0982  -72.0864  -84.2320  0.525017  0.829568  2012  \n",
       "368   -1.4100  -17.4595 -130.9577 -121.6043  0.429067  0.136592  2012  \n",
       "369   -1.4100  -17.4595 -130.9577 -121.6043  0.429067  0.136592  2012  \n",
       "370   -1.4100  -17.4595 -130.9577 -121.6043  0.429067  0.136592  2012  \n",
       "\n",
       "[369 rows x 14 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[df.isnull().any(axis = 1)]\n",
    "df = df.dropna()\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5. 编写程序计算各公司历年来平均的营收增长率；并增加一列“平均营收增加率”来存储该数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "new_df = df.reset_index(drop = True)\n",
    "g = new_df.groupby([\"公司代码\",\"year\"])[\"营收增长率\"].mean()\n",
    "g = g.to_frame().reset_index()\n",
    "g.columns = [\"公司代码\",\"year\", \"平均营收增长率\"]\n",
    "fin_df = pd.merge(new_df, g, on = [\"公司代码\",\"year\"], how = \"outer\")\n",
    "fin_df\n",
    "fin_df.to_csv(\"test.csv\", encoding = \"GB2312\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
